using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using XFGameFramework;

public class 


PlayerController : Controller
{
    public Action OnPlayerDead;

    public Action OnStrengthDepleted;
    public Action OnNeatnessDepleted;
    private PlayerModel playerModel;
    private GameSaveController gameSaveController => Module.LoadController<GameSaveController>();
    private GameController gameController => Module.LoadController<GameController>();
    public PlayerModel GetPlayerModel()
    {
        playerModel = gameSaveController.GetPlayerModel(0);
        
        return playerModel;
    }
    
    /// <summary>
    /// 重新引用,防止引用丢失
    /// </summary
    public void Reload()
    {
        playerModel = gameSaveController.GetPlayerModel(0);
    }

    // public enemy GetEnemyModel()
    // {
    //     enemyModel = Module.GetModel<EnemyModel>();
    //     if (enemyModel == null)
    //     {
    //         enemyModel = new EnemyModel();
    //         Module.AddModel(enemyModel);
    //     }
    //     return enemyModel;
    // }

    #region 方法
    public void Set(PlayerModel other)
    {
        if (other == null) return;
        playerModel.currentHp = other.currentHp;
        playerModel.maxHp = other.maxHp;
        playerModel.strength = other.strength;
        playerModel.neatness = other.neatness;

    }
    /// <summary>
    /// 判断玩家是否死亡
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    public bool IsDead(PlayerModel model)
    {
        if (model.currentHp <= 0)
        {
            model.currentHp = 0;
            return true;
        }

        return false;
    }
    #endregion

    #region 属性
    public int CurrentHp
    {
        get => playerModel.currentHp;
        set
        {
            playerModel.currentHp = Mathf.Max(0, value);//玩家生命为非负数
            playerModel.currentHp = Mathf.Min(value, playerModel.maxHp);//玩家生命不超过最大生命值
            if (playerModel.currentHp == 0)
            {   
                gameController.SetGameState(GameState.Fight_Failture);
                OnPlayerDead?.Invoke();
            }
        }
    }
    public int MaxHp
    {
        get => playerModel.maxHp;
        set => playerModel.maxHp = value;
    }
   
    public int CurrentStrength
    {
        get => playerModel.currentStrength;
        set
        {
            playerModel.currentStrength = Mathf.Max(0, value);//玩家力量为非负数
            playerModel.currentStrength = Mathf.Min(value, playerModel.strength);//玩家力量不超过最大力量值
            if (playerModel.currentStrength <= 0)
            {
                OnStrengthDepleted?.Invoke();
            }
        }
    }
    public int CurrentNeatness
    {
        get => playerModel.currentNeatness;
        set
        {
            playerModel.currentNeatness = Mathf.Max(0, value);//玩家整洁度为非负数
            playerModel.currentNeatness = Mathf.Min(value, playerModel.neatness);//玩家整洁度不超过最大整洁度值
            if (playerModel.currentNeatness <= 0)
            {
                OnNeatnessDepleted?.Invoke();
            }
        }
    }
    public string name
    {
        get => playerModel.name;
        set => playerModel.name = value;
    }

    public int level
    {
        get => playerModel.level;
        set => playerModel.level = value;
    }
    #endregion
    public override void OnInit()
    {
        base.OnInit();
        playerModel = gameSaveController.GetPlayerModel(0);
    }
    public List<int> GetSkillList()
    {
        Debug.Log("GetSkillList"+playerModel.skillsID);
        return playerModel.skillsID;
    }
    public List<int> GetSkillCd()
    {
        return playerModel.skillsCD;
    }
    public void addskill(int skillid)
    {
        playerModel.skillsID.Add(skillid);
    }
    
  
}
